//
//  ByAddAddressVC.m
//  hutouben_ios
//
//  Created by mc on 16/11/29.
//  Copyright © 2016年 mc. All rights reserved.
//

#import "ByAddAddressVC.h"
#import "ZKCityPickerView.h"

#import "ByTextView.h"
#import "ByAddressModel.h"

@interface ByAddAddressVC ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate,UITextViewDelegate,ZKCityPickerViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) ZKCityPickerView *cityView;

@property (nonatomic, strong) NSArray *titlesAry;
@property (nonatomic, strong) NSArray *placeAry;
@property (nonatomic, strong) UISwitch *defaultSwitch;

@end

@implementation ByAddAddressVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = self.type == 0?@"新增地址":@"修改地址";
    [self addLeftBackBarButtonItem];
    [self addBYItemviews:@[@"保存"] mode:ByNavigationItemModeText position:ByNavigationItemPositionModeRight];
    [self initTableView];
    [self initCityView];
}
#pragma mark ---getter
-(NSArray *)titlesAry{
    if (_titlesAry == nil) {
        if (self.type == 0 || [self.alertModel.is_default isEqualToString:@"0"]) {
            _titlesAry = @[@"收货人",@"手机号码",@"选择地区",@"详细地址",@"设为默认"];

        }else{
            _titlesAry = @[@"收货人",@"手机号码",@"选择地区",@"详细地址"];
        }
    }
    return _titlesAry;
}

-(NSArray *)placeAry{
    if (_placeAry == nil) {
//        if (self.type == 0) {
           _placeAry = @[@"收货人的姓名",@"11位手机号",@"所在地区",@"详细地址"];

//        }
//    else{
//            _placeAry = @[self.alertModel.contactname,self.alertModel.mobile,self.alertModel.province,self.alertModel.detailinfo];
//        }
    }
    return _placeAry;
}

-(UISwitch *)defaultSwitch{
    
    if (!_defaultSwitch) {
        _defaultSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(0, 0, 40, 20)];
//        if ([self.alertModel.is_default isEqualToString:@"1"]) {
//            [_defaultSwitch setOn:YES];
//        }else{
//            [_defaultSwitch setOn:NO];
        
//        }
        
    }
    return _defaultSwitch;
}

#pragma mark ---创建视图
-(void)initTableView{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64)];
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = HTB_BG_GRAY;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.view.backgroundColor = HTB_BG_GRAY;
    
    _tableView.tableFooterView = [[UIView alloc] init];
}

/***创建城市选择器*/
- (void)initCityView{
    self.cityView = [[ZKCityPickerView alloc] initWithPicker];
    self.cityView.delegate = self;
    [self.view addSubview:self.cityView];
}
#pragma mark ----UITableViewDelegate,UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.titlesAry.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *indentier = @"addAddressCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    CGFloat maxWidth = 0;
    /***循环寻找最长的title*/
    for (NSString *str in self.titlesAry) {
        CGFloat width = [str getSizeOfString:16].width;
        maxWidth = width > maxWidth ? width:maxWidth;
    }
    for (UIView *subview in cell.subviews) {
        [subview removeFromSuperview];
    }
    
    CGFloat x = 15;
    //title
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, 10, maxWidth + 10, 30)];
    titleLabel.textColor = HTB_TEXT_COLOR1;
    titleLabel.font = FONT(16);
    titleLabel.text = self.titlesAry[indexPath.row];
    [cell addSubview:titleLabel];
    
    //线
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 49, SCREEN_WIDTH, 1)];
    lineView.backgroundColor = HTB_LINE_COLOR;
    [cell addSubview:lineView];
    
    x = titleLabel.right + 18;
    /***选择地区*/
    if (indexPath.row == 2) {
        UILabel *infoLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, 10, SCREEN_WIDTH - x, 30)];
        infoLabel.font = FONT(14);
        infoLabel.tag = indexPath.row + 10;
        [cell addSubview:infoLabel];

        if (self.type == 0) {
            infoLabel.text = self.placeAry[indexPath.row];
            infoLabel.textColor = [UIColor lightGrayColor];
        }else{
            infoLabel.text = self.alertModel.province;
            infoLabel.textColor = HTB_TEXT_COLOR2;
        }

        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(areaSelect:)];
        infoLabel.userInteractionEnabled = YES;
        [infoLabel addGestureRecognizer:tap];
        
    }
    /***详细地址*/
    else if (indexPath.row == 3) {
        UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(titleLabel.right + 10, 0, SCREEN_WIDTH - titleLabel.right - 10, 50)];
        ByTextView *textView = [[ByTextView alloc] initWithFrame:CGRectMake(0, 10,SCREEN_WIDTH - titleLabel.right - 10, 30)];
        textView.font = FONT(14);
        textView.textColor = HTB_TEXT_COLOR1;
        textView.tag = 10 + indexPath.row;
        textView.placeholder = self.placeAry[indexPath.row];
        textView.delegate = self;
        [bgView addSubview:textView];
        [cell addSubview:bgView];
        
        if (self.type == 1) {
            textView.text = self.alertModel.detailinfo;
        }
    }
    /**设为默认*/
    else if (indexPath.row == 4){
        cell.accessoryView = self.defaultSwitch;
    }
    
    else{
        UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(x, 10, SCREEN_WIDTH - x , 30)];
        tf.font = FONT(14);
        tf.textColor = HTB_TEXT_COLOR1;
        tf.placeholder = self.placeAry[indexPath.row];
        tf.tag = 10 + indexPath.row;
        tf.delegate = self;
        [cell addSubview:tf];
        if (indexPath.row == 1) {
            tf.keyboardType = UIKeyboardTypeNumberPad;
        }
        if (self.type == 1) {
            NSArray *dataAry = @[self.alertModel.contactname,self.alertModel.mobile];

            tf.text = dataAry[indexPath.row];
        }
        
    }
    
    return cell;


}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 20;
}

#pragma mark ---地区选择代理
/**选择地区*/
- (void)areaSelect:(UITapGestureRecognizer *) tap{
    [_cityView open];
}

- (void)cityPickerViewDidChooseAddress:(CityObject *)cityModel{
    UITableViewCell *cell = [_tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:0]];
    UILabel *label = [cell viewWithTag:12];
    label.text = [NSString stringWithFormat:@"%@%@%@",cityModel.province,cityModel.city,cityModel.area];
}
@end
